package com.BigDate.server.service.impl;

import com.BigDate.server.mapper.UserMapper;
import com.BigDate.server.pojo.RespBean;
import com.BigDate.server.pojo.RespPageBean;
import com.BigDate.server.pojo.User;
import com.BigDate.server.service.IUserService;
import com.BigDate.server.utils.Md5Util;
import com.BigDate.server.utils.pyUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author stx
 * @since 2021-12-22
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Resource
    private UserMapper userMapper;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @Override
    public User userLogin(String username, String password) {
        return userMapper.userLogin(username, Md5Util.StringInMd5(password));
    }

    @Override
    public RespBean Login(String username, String password, String code, HttpServletRequest request) {
        String captcha = (String) request.getSession().getAttribute("captcha");
        if(StringUtils.isEmpty(code) || !captcha.equalsIgnoreCase(code)){
            return RespBean.error("验证码输入错误，请重新输入!");
        }

        User user = userMapper.selectOne(new QueryWrapper<User>()
                .eq("username", username)
                .eq("password", Md5Util.StringInMd5(password)));
        if(!Md5Util.StringInMd5(password).equals(user.getPassword())){
            return RespBean.error("用户名或密码错误");
        }
        user.setPassword(null);
        String port = "";
        while(true) {
            port = pyUtils.initPort();
            if(pyUtils.checkPort(port)==false){
                break;
            }
        }
        System.out.println("当前用户的端口号为:"+port);
        user.setPort(port);
        redisTemplate.opsForValue().set("port",port);
        return RespBean.success("登录成功",user);
    }

    @Override
    public int updatePwd(User user) {
        return userMapper.updatePwd(user);
    }

    @Override
    public IPage<User> findAll(Page<User> page) {
        return userMapper.findAll(page);
    }

    @Override
    public User findById(Integer id) {
        System.out.println(id);
        System.out.println(1);
        return userMapper.findById(id);
    }

    @Override
    public int deleteById(Integer id) {
        return userMapper.deleteById(id);
    }

    @Override
    public int update(User user) {
        return userMapper.update(user);
    }

    @Override
    public int add(User user) {
        return userMapper.add(user);
    }

    @Override
    public RespPageBean getUserByPage(Integer currentPage, Integer size, User user) {
        Page<User> page = new Page<>(currentPage,size);
        IPage<User> studentByPage = userMapper.getUserByPage(page,user);
        RespPageBean respPageBean = new RespPageBean(studentByPage.getTotal(),studentByPage.getRecords());
        return respPageBean;
    }
}
